<template>
  <el-header :style="'margin-bottom:'+ headerBottom +'px'+';'">
    <h3 class="logo"><svg-icon icon-class="EarOfWheat" />  Blog</h3>
    <div class="bg-purple-light">
      <el-menu :default-active="activeIndex" router class="el-menu-demo" mode="horizontal" style="border: none;"
        background-color="rgba(0,0,0,0)" text-color="#fff" active-text-color="#ffffff">
        <el-menu-item index="/"><i class="el-icon-s-home" style="color: rgba(255, 255, 255);"></i>首页
        </el-menu-item>
        <el-menu-item :index="item.path" v-for="item in menulist" :key="item.id">
          <!--                图标-->
          <i :class="item.icon" style="color: rgba(255, 255, 255);"></i>
          <!--                文本-->
          {{item.authName}}
        </el-menu-item>
      </el-menu>
    </div>
    <div class="bg-purple-light el-menu-hidden" v-if="menuHiddenVisiable">
      <el-menu :default-active="activeIndex" router background-color="rgba(84,92,100,0.5)" text-color="#fff"
        active-text-color="#ffffff">
        <el-menu-item index="/" @click="menuAway"><i class="el-icon-s-home"
            style="color: rgba(255, 255, 255);"></i>首页</el-menu-item>
        <el-menu-item :index="item.path" v-for="item in menulist" :key="item.id" @click="menuAway">
          <!--                图标-->
          <i :class="item.icon" style="color: rgba(255, 255, 255);"></i>
          <!--                文本-->
          {{item.authName}}
        </el-menu-item>
      </el-menu>
    </div>

    <div class="menu-expend" @click="menuExpend">
      <i class="el-icon-menu" style="color: rgba(255, 255, 255);"></i>
    </div>

    <div v-show="islogin" class="bg-purple">
      <el-dropdown class="avatar-container right-menu-item hover-effect" trigger="click">
        <div class="avatar-wrapper">
          <el-avatar class="user-avatar" :src="user.avatar" @error="errorHandler">
            <i class="el-icon-s-custom" />
          </el-avatar>
          <p class="avatar-Name">{{user.userName}}</p>
        </div>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item @click.native="goToProfile">
            <i class="el-icon-user"></i>
            <span>个人中心</span>
          </el-dropdown-item>
          <el-dropdown-item divided @click.native="logout">
            <i class="el-icon-switch-button"></i>
            <span>退出登录</span>
          </el-dropdown-item>
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div v-show="!islogin" class="bg-purple">
      <div class="avatar-wrapper">
        <!-- <el-avatar class="avatar" src="avatar" @error="errorHandler">
            <i class="el-icon-s-custom" @click="tologin"/>
          </el-avatar> -->
        <p class="avatar-Name" @click="tologin">登录|注册</p>
      </div>
    </div>
  </el-header>
</template>

<script>
  import 'element-ui/lib/theme-chalk/display.css'
  import {
    getToken
  } from '@/utils/auth'
  export default {
    name: 'workbenchNavBar',
    data() {
      return {
        islogin: false,
        activeIndex: 'this.$router.path',
        menuHiddenVisiable: false,
        headerBottom: 0,
        searchList: [],
        menulist: [
          {
            id: 1,
            authName: '随笔',
            path: '/cmsEssay',
            icon: 'el-icon-edit',
          },
          {
            id: 2,
            authName: '留言',
            path: '/cmsMessage',
            icon: 'el-icon-chat-dot-round',
          },
          {
            id: 3,
            authName: '分类',
            path: '/categories',
            icon: 'el-icon-collection',
          },
          {
            id: 4,
            authName: '标签',
            path: '/tags',
            icon: 'el-icon-price-tag',
          },
          {
            id: 5,
            authName: '归档',
            path: '/archive',
            icon: 'el-icon-date',
          },
          {
            id: 6,
            authName: '搜索',
            path: '/search',
            icon: 'el-icon-search',
          },
          {
            id: 7,
            authName: '关于',
            path: '/about',
            icon: 'el-icon-user',
          },
          {
            id: 8,
            authName: '友链',
            path: '/friends',
            icon: 'el-icon-link',
          },
          {
            id: 9,
            authName: '时间轴',
            path: '/timeline',
            icon: 'el-icon-time',
          },
          {
            id: 10,
            authName: '照片墙',
            path: '/gallery',
            icon: 'el-icon-picture',
          },
        ],
        user: {},
      };
    },
    computed: {

    },
    watch: {

    },
    created() {

    },
    mounted() {
      this.login();
    },
    methods: {
      menulistAdd() {
        // push()方法一般是添加到数组的最后的位置；unshift()方法是往最前面的位置添加。
        // this.menulist.push({id:"",authName:""})
        this.menulist.unshift({
          id: "",
          authName: ""
        })
      },
      // 展开菜单栏
      menuExpend() {
        this.menuHiddenVisiable = !this.menuHiddenVisiable
        if (this.menuHiddenVisiable === true) {
          this.headerBottom = (this.menulist.length + 1) * 56
        } else {
          this.headerBottom = 0
        }
      },
      // 收起菜单
      menuAway() {
        this.menuHiddenVisiable = false;
        this.headerBottom = 0;
      },
      tologin() {
        this.$router.push({
          path: "/cmsLogin"
        });
      },
      login() {
        let token = getToken();
        if (token == null || token == '') {
          this.islogin = false;
        } else {
          const userString = localStorage.getItem('userL');
          const user = userString ? JSON.parse(userString) : {};
          this.user = user
          this.islogin = true;
        }
      },
      errorHandler() {
        return true
      },
      goToProfile() {
        this.$router.push({
          path: '/profile'
        });
      },
      async logout() {
        this.$confirm('确定注销并退出系统登录吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$store.dispatch('LogOut').then(() => {
            location.href = '/';
          })
        }).catch(() => {});
      }
    }
  }
</script>

<style scoped>
  .el-header {
    display: flex;
    justify-content: space-between;
    background-color: rgba(0, 0, 0, 0.2);
    align-items: center;
    transition: .2s;
  }

  .el-header:hover {
    opacity: 1 !important;
  }

  .el-menu {
    flex-shrink: 0;
    background-color: rgba(0, 0, 0, 0) !important;
  }

  .el-menu /deep/ .el-menu-item {
    background-color: rgba(0, 0, 0, 0) !important;
    border-bottom: none !important;
  }


  .el-menu /deep/ .el-menu-item i {
    color: rgba(255, 255, 255);
  }

  .el-menu /deep/ .el-menu-item:hover i {
    color: white;
  }

  .el-menu /deep/ .el-menu-item:hover {
    background-color: rgba(0, 0, 0, 0.5) !important;
  }

  .search_input {
    float: right;
    position: relative;
    box-sizing: border-box;
    width: 50vh;
  }

  .search-blog {
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 1;
    -webkit-box-orient: vertical;
    padding-left: 20px;
    padding-right: 20px;
  }

  .bg-purple-light {
    float: right;
  }

  .bg-purple {
    float: right;
  }

  .user-avatar {
    float: left;
    cursor: pointer;
    border: dashed rgba($color: #ffff7f, $alpha: 0.5);
  }

  .avatar-container {
    margin-right: 30px;
  }

  .avatar-wrapper {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .avatar-Name {
    margin-left: 10px;
    cursor: pointer;
    float: right;
    font-size: 16px;
    color: #ffffff;
  }

  .logo {
    float: left;
    color: #ffd04b;
    font-weight: bold;
  }

  .logo:hover {
    cursor: pointer;
  }

  .el-menu-hidden {
    /*display: none;*/
    position: absolute;
    top: 60px;
    left: 0;
    border-top: 1px solid #ccc;
    border-right: none;
    width: 100%;
  }

  .menu-expend {
    display: none !important;
  }

  /* 窗口可视区域小于1000隐藏搜索框 */
  @media screen and (max-width: 1000px) {
    .search_input {
      display: none;
    }
  }

  @media screen and (max-width: 768px) {
    .el-menu /deep/ .el-menu-item {
      background-color: rgba(0, 0, 0, 0.3) !important;
    }

    .el-menu-demo {
      display: none;
    }

    .menu-expend {
      display: block !important;
      float: right;
    }

    .menu-expend:hover {
      color: #ffd04b;
      cursor: pointer;
    }
  }
  .unread-text {
    height: 20px;
  	position: absolute;
  	background-color: #F56C6C;
  	left: 35px;
  	top: 11px;
  	color: white;
  	border-radius: 30px;
  	padding: 0 5px;
  	font-size: 12px;
  	text-align: center;
  	white-space: nowrap;
  	border: 1px solid #f1e5e5;
    /* 以下属性确保完美居中 */
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 20px; /* 确保圆形最小宽度与高度相同 */
    box-sizing: border-box; /* 包含边框和内边距在尺寸内 */
  }
</style>
